diff --git includes/form.inc includes/form.inc
index 5a81c24..3d1800e 100644
--- includes/form.inc
+++ includes/form.inc
@@ -193,7 +193,7 @@ function drupal_build_form($form_id, &$form_state) {
       // Record the filepath of the include file containing the original form,
       // so the form builder callbacks can be loaded when the form is being
       // rebuilt from cache on a different path (such as 'system/ajax'). See
-      // form_get_cache(). 
+      // form_get_cache().
       // $menu_get_item() is not available at installation time.
       if (!isset($form_state['build_info']['file']) && !defined('MAINTENANCE_MODE')) {
         $item = menu_get_item();
@@ -1293,9 +1293,13 @@ function form_builder($form_id, $element, &$form_state) {
 
     // Don't squash existing parents value.
     if (!isset($element[$key]['#parents'])) {
-      // Check to see if a tree of child elements is present. If so,
-      // continue down the tree if required.
-      $element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key);
+      $element[$key]['#parents'] = $element['#parents'];
+      // Check to see if a tree of child elements is present. If not, remove
+      // the parent element from #parents, but keep any other ancestors.
+      if (!$element['#tree']) {
+        array_pop($element[$key]['#parents']);
+      }
+      $element[$key]['#parents'][] = $key;
     }
     // Ensure #array_parents follows the actual form structure.
     $array_parents = isset($element['#array_parents']) ? $element['#array_parents'] : array();
diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test
index 842f325..283be8d 100644
--- modules/simpletest/tests/form.test
+++ modules/simpletest/tests/form.test
@@ -978,7 +978,6 @@ class FormsClickedButtonTestCase extends DrupalWebTestCase {
   }
 }
 
-
 /**
  * Tests rebuilding of arbitrary forms by altering them.
  */
@@ -1043,3 +1042,32 @@ class FormsArbitraryRebuildTestCase extends DrupalWebTestCase {
     $this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.');
   }
 }
+
+/**
+ * Tests the #tree property.
+ */
+class FormsTreeValueTestCase extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Form values tree',
+      'description' => 'Tests using the tree property to build the form values.',
+      'group' => 'Form API',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('form_test');
+  }
+
+  /**
+   * Tests the tree value form to make sure the value is under the right parent.
+   */
+  function testTreeValues() {
+    $edit = array(
+      'parent[value]' => 'DEFAULT',
+    );
+    $this->drupalPost('form-test/tree-values', $edit, t('Save'));
+    $this->assertText('Value is under parent.');
+  }
+}
diff --git modules/simpletest/tests/form_test.module modules/simpletest/tests/form_test.module
index fb01444..0f9a63a 100644
--- modules/simpletest/tests/form_test.module
+++ modules/simpletest/tests/form_test.module
@@ -126,6 +126,14 @@ function form_test_menu() {
     'type' => MENU_CALLBACK,
   );
 
+  $items['form-test/tree-values'] = array(
+    'title' => 'Tree values test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('form_test_tree_values'),
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
+
   return $items;
 }
 
@@ -1035,3 +1043,36 @@ function form_test_user_register_form_rebuild($form, &$form_state) {
   drupal_set_message('Form rebuilt.');
   $form_state['rebuild'] = TRUE;
 }
+
+/**
+ * Form constructor for the #tree property test form.
+ */
+function form_test_tree_values($form, $form_state) {
+  $form['parent'] = array(
+    '#title' => 'Parent',
+    '#type' => 'fieldset',
+    '#tree' => TRUE,
+  );
+  $form['parent']['wrapper'] = array(
+    '#type' => 'container',
+    '#tree' => FALSE,
+  );
+  $form['parent']['wrapper']['value'] = array(
+    '#type' => 'textfield',
+    '#default_value' => 'DEFAULT',
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+  return $form;
+}
+
+/**
+ * Submit callback for the #tree property test form.
+ */
+function form_test_tree_values_submit($form, $form_state) {
+  if ($form_state['values']['parent']['value'] == 'DEFAULT') {
+    drupal_set_message('Value is under parent.');
+  }
+}
