Flatten Binary Tree to Linked List leetcode java

题目:

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        /        2   5
      / \        3   4   6

The flattened tree should look like:

   1
         2
             3
                 4
                     5
                         6

题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空。注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子。整个程序需要维护一个全局变量,保存当前所遍历的节点。

代码如下:

1   TreeNode lastvisited = null;
 2     public void flatten(TreeNode root) {
 3         if(root == null)
 4             return;
 5         
 6         TreeNode realright = root.right;
 7         if(lastvisited != null){
 8             lastvisited.left = null;
 9             lastvisited.right = root;
10         }
11         
12         lastvisited = root;
13         flatten(root.left);
14         flatten(realright);
15     }

Reference:http://blog.csdn.net/perfect8886/article/details/20000083

此题还有不用递归方法解决的方法,那就是使用栈。对整棵树一直向右子树方向遍历。当遍历的节点有右孩子时,就将其入栈。有左孩子时,将其更新为当前节点的右孩子,左孩子置空。当左孩子为空时而栈不空时,就弹出栈,作为右孩子。代码如下:

1     public void flatten(TreeNode root) {
 2         Stack<TreeNode> stack = new Stack<TreeNode>();
 3         TreeNode p = root;
 4  
 5         while(p != null || !stack.empty()){
 6  
 7             if(p.right != null){
 8                 stack.push(p.right);
 9             }
10  
11             if(p.left != null){
12                 p.right = p.left;
13                 p.left = null;
14             }else if(!stack.empty()){
15                 TreeNode temp = stack.pop();
16                 p.right=temp;
17             }
18  
19             p = p.right;
20         }
21     }

Reference: //http://www.programcreek.com/2013/01/leetcode-flatten-binary-tree-to-linked-list/

Flatten Binary Tree to Linked List leetcode java,布布扣,bubuko.com

时间: 2024-10-13 11:05:02

Flatten Binary Tree to Linked List leetcode java的相关文章

Flatten Binary Tree to Linked List &lt;leetcode&gt;

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 算法:该题用的是递归,只要考虑当前节点,思路如下,设当前节点为root,把root的左节点的最右节点的右子树指向root的右子树,然后把root的右指针指向root的左子树,root的做指针置空,下一个节点处理r

Flatten Binary Tree to Linked List ——LeetCode

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题目大意:给一个二叉树,将它转为一个list,转换后的序列应该是先序遍历,list由这棵树的右孩子表示. 解题思路:递归的处理左子树,然后处理右子树,将左孩子的右孩子置为当前节点的右孩子,然后将当前节点的右孩子置

【LeetCode】Flatten Binary Tree to Linked List

题目:Flatten Binary Tree to Linked List <span style="font-size:18px;">/**LeetCode Flatten Binary Tree to Linked List * 题意:给定一个二叉树,将其转变为一个相当于单链表的结构,观察可知该结构即:每一个节点左儿子为空,右儿子指向自己先序遍历时的下一个节点 * 思路:有观察可得,应对其进行先序遍历,得到正确的序列连接起来 * Definition for binar

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6Hints: If you notice carefully

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

LeetCode: Flatten Binary Tree to Linked List [114]

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to show hints. [题意] 将一颗二叉树转化为链表,right充当next指针,元素顺序为先序遍历的循序.不使用额外的空间 [思路] 先对左子树链表化,然后将右子树链表化.然后

[Leetcode][Tree][Flatten Binary Tree to Linked List ]

按照前序遍历的顺序把树用right连起来. 本来想了半天,一点思路都没有,总觉得Inplace的解法一般都非常巧妙. 后来我突发灵感,决定用一个变量记录当前访问到哪个点,真是太机智了~~ 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), r

[LeetCode 题解]: Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a