-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path剑指offer_树.txt
More file actions
75 lines (67 loc) · 2.19 KB
/
剑指offer_树.txt
File metadata and controls
75 lines (67 loc) · 2.19 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
65
66
67
68
69
70
71
72
73
74
75
// 二叉搜索树转化为双向链表
// 要点是中序遍历到一个节点的时候,将它的左指针指向前一个节点,将上一个节点的右指针指向它,更新pre
public class Solution {
TreeNode pre = null;
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null) return null;
ConvertSub(pRootOfTree);
while(pre.left != null) pre = pre.left;
return pre;
}
public void ConvertSub(TreeNode pRootOfTree){
if(pRootOfTree == null) return;
ConvertSub(pRootOfTree.left);
pRootOfTree.left = pre;
if(pre != null) pre.right = pRootOfTree;
pre = pRootOfTree;
ConvertSub(pRootOfTree.right);
}
}
// 非递归实现
import java.util.Stack;
public class Solution {
public TreeNode Convert(TreeNode pRootOfTree) {
TreeNode head = null;
TreeNode pre = null;
Stack<TreeNode> stack = new Stack<>();
while(pRootOfTree != null || !stack.isEmpty()){
while(pRootOfTree != null){
stack.push(pRootOfTree);
pRootOfTree = pRootOfTree.left;
}
pRootOfTree = stack.pop();
if(head == null){
head = pRootOfTree;
pre = pRootOfTree;
}else{
pre.right = pRootOfTree;
pRootOfTree.left = pre;
pre = pRootOfTree;
}
pRootOfTree = pRootOfTree.right;
}
return head;
}
}
//求数的深度:简化版递归
public int maxDeepth(TreeNode root){
if(root == null)
return 0;
return 1 + Math.max(maxDeepth(root.left), maxDeepth(root.right));
}
//平衡二叉树的判定
// 直接跟据定义用递归就可以搞定
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) {
return true;
}
return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 &&
IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
//