-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlattenBinaryTreeToLinkedList.java
More file actions
64 lines (59 loc) · 1.39 KB
/
FlattenBinaryTreeToLinkedList.java
File metadata and controls
64 lines (59 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import javax.swing.tree.TreeNode;
/**
* 题目:将给定的二叉树转化为“只有右孩子节点”的链表。(前序遍历的方式)
* 例如:
* Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
* 解题思路:
* 首先判断左子树是否为空,不为空就寻找到树根的左孩子节点;
* 然后寻找该节点是否有右孩子,如果有继续寻找,直到找到属于叶子节点的右孩子;
* 此时,该节点的右子树“指向”当前树的右子树,并将当前左子树变为树根的右孩子,将整棵树左孩子置为空。
* 最后,根节点“指向”根节点的右孩子,继续上述操作,直到整棵树遍历完即得到结果。
*/
public class FlattenBinaryTreeToLinkedList {
}
class TreeNode25
{
int val;
TreeNode25 left;
TreeNode25 right;
TreeNode25(int x){val=x;}
}
class Solution131
{
public void flatten(TreeNode25 root)
{
if(root==null) return;
while(root!=null)
{
if(root.left!=null)
{
TreeNode25 cur=root.left;
//寻找最右边的元素
while(cur.right!=null)
cur=cur.right;
//将左子树插入到右子树中
cur.right=root.right;
root.right=root.left;
root.left=null;
}
root=root.right;
}
}
}