-
Notifications
You must be signed in to change notification settings - Fork 0
/
interviewBiNode.java
65 lines (61 loc) · 1.88 KB
/
interviewBiNode.java
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
import java.util.LinkedList;
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
TreeNode prev=new TreeNode(0);
// 1,画图分析可以发现,需要对原树左中序遍历
// 2,如何保证原址呢?哨兵指针
public TreeNode convertBiNode(TreeNode root) {
if(root==null){
return null;
}
TreeNode head=prev;
centerNode(root);
return head.right;
}
public void centerNode(TreeNode root){
if(root==null){
return;
}
centerNode(root.left);
//prev.left=null; 不要这样的代码!prev.right.left=null,其实就是root.left=null
root.left=null;
prev.right=root;
prev=root;
centerNode(root.right);
}
}
// https://leetcode-cn.com/problems/binode-lcci/
class Solution {
public TreeNode convertBiNode(TreeNode root) {
// 1,画图分析可以发现,需要对原树左中序遍历
// 2,如何保证原址呢?
// ---->先用一个链表把链表存下来. 可是不知道前驱节点,显示不行,还是选择递归
LinkedList<TreeNode> linkedlist = new LinkedList<>();
// getLinkedList(list, root);
// while (!linkedlist.isEmpty()) {
// TreeNode p = linkedlist.poll();
// root=p.val;
// root.left=null;
// root.right=p
// }
}
// public void getLinkedList(LinkedList<TreeNode> list, TreeNode root) {
// if (root == null) {
// return;
// }
// getLinkedList(list, root.left);
// list.add(root);
// getLinkedList(list, root.right);
// }
}