|
44 | 44 |
|
45 | 45 | <!-- 这里可写通用的实现逻辑 -->
|
46 | 46 |
|
| 47 | +由于二叉搜索树的性质,可以利用中序遍历得到递增序列 |
| 48 | + |
47 | 49 | <!-- tabs:start -->
|
48 | 50 |
|
49 | 51 | ### **Python3**
|
50 | 52 |
|
51 | 53 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 54 |
|
53 | 55 | ```python
|
54 |
| - |
| 56 | +# Definition for a binary tree node. |
| 57 | +# class TreeNode: |
| 58 | +# def __init__(self, val=0, left=None, right=None): |
| 59 | +# self.val = val |
| 60 | +# self.left = left |
| 61 | +# self.right = right |
| 62 | +class Solution: |
| 63 | + def increasingBST(self, root: TreeNode) -> TreeNode: |
| 64 | + head, tail = None, None |
| 65 | + stack = [] |
| 66 | + cur = root |
| 67 | + while stack or cur: |
| 68 | + while cur: |
| 69 | + stack.append(cur) |
| 70 | + cur = cur.left |
| 71 | + cur = stack.pop() |
| 72 | + if not head: |
| 73 | + head = cur |
| 74 | + else: |
| 75 | + tail.right = cur |
| 76 | + tail = cur |
| 77 | + cur.left = None |
| 78 | + cur = cur.right |
| 79 | + return head |
55 | 80 | ```
|
56 | 81 |
|
57 | 82 | ### **Java**
|
58 | 83 |
|
59 | 84 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 85 |
|
61 | 86 | ```java
|
| 87 | +/** |
| 88 | + * Definition for a binary tree node. |
| 89 | + * public class TreeNode { |
| 90 | + * int val; |
| 91 | + * TreeNode left; |
| 92 | + * TreeNode right; |
| 93 | + * TreeNode() {} |
| 94 | + * TreeNode(int val) { this.val = val; } |
| 95 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 96 | + * this.val = val; |
| 97 | + * this.left = left; |
| 98 | + * this.right = right; |
| 99 | + * } |
| 100 | + * } |
| 101 | + */ |
| 102 | +class Solution { |
| 103 | + public TreeNode increasingBST(TreeNode root) { |
| 104 | + TreeNode head = null, tail = null; |
| 105 | + Deque<TreeNode> stack = new ArrayDeque<>(); |
| 106 | + TreeNode cur = root; |
| 107 | + while (!stack.isEmpty() || cur != null) { |
| 108 | + while (cur != null) { |
| 109 | + stack.push(cur); |
| 110 | + cur = cur.left; |
| 111 | + } |
| 112 | + cur = stack.pop(); |
| 113 | + if (head == null) { |
| 114 | + head = cur; |
| 115 | + } else { |
| 116 | + tail.right = cur; |
| 117 | + } |
| 118 | + tail = cur; |
| 119 | + cur.left = null; |
| 120 | + cur = cur.right; |
| 121 | + } |
| 122 | + return head; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### **Go** |
| 128 | + |
| 129 | +```go |
| 130 | +/** |
| 131 | + * Definition for a binary tree node. |
| 132 | + * type TreeNode struct { |
| 133 | + * Val int |
| 134 | + * Left *TreeNode |
| 135 | + * Right *TreeNode |
| 136 | + * } |
| 137 | + */ |
| 138 | +func increasingBST(root *TreeNode) *TreeNode { |
| 139 | + var head, tail *TreeNode |
| 140 | + stack := make([]*TreeNode, 0) |
| 141 | + cur := root |
| 142 | + for len(stack) > 0 || cur != nil { |
| 143 | + for cur != nil { |
| 144 | + stack = append(stack, cur) |
| 145 | + cur = cur.Left |
| 146 | + } |
| 147 | + cur = stack[len(stack)-1] |
| 148 | + stack = stack[:len(stack)-1] |
| 149 | + if head == nil { |
| 150 | + head = cur |
| 151 | + } else { |
| 152 | + tail.Right = cur |
| 153 | + } |
| 154 | + tail = cur |
| 155 | + cur.Left = nil |
| 156 | + cur = cur.Right |
| 157 | + } |
| 158 | + return head |
| 159 | +} |
| 160 | +``` |
62 | 161 |
|
| 162 | +### **C++** |
| 163 | + |
| 164 | +```cpp |
| 165 | +/** |
| 166 | + * Definition for a binary tree node. |
| 167 | + * struct TreeNode { |
| 168 | + * int val; |
| 169 | + * TreeNode *left; |
| 170 | + * TreeNode *right; |
| 171 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 172 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 173 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 174 | + * }; |
| 175 | + */ |
| 176 | +class Solution { |
| 177 | +public: |
| 178 | + TreeNode* increasingBST(TreeNode* root) { |
| 179 | + TreeNode *head = nullptr, *tail = nullptr; |
| 180 | + stack<TreeNode*> stk; |
| 181 | + TreeNode* cur = root; |
| 182 | + while (!stk.empty() || cur != nullptr) { |
| 183 | + while (cur != nullptr) { |
| 184 | + stk.push(cur); |
| 185 | + cur = cur->left; |
| 186 | + } |
| 187 | + cur = stk.top(); |
| 188 | + stk.pop(); |
| 189 | + if (head == nullptr) { |
| 190 | + head = cur; |
| 191 | + } else { |
| 192 | + tail->right = cur; |
| 193 | + } |
| 194 | + tail = cur; |
| 195 | + cur->left = nullptr; |
| 196 | + cur = cur->right; |
| 197 | + } |
| 198 | + return head; |
| 199 | + } |
| 200 | +}; |
63 | 201 | ```
|
64 | 202 |
|
65 | 203 | ### **...**
|
|
0 commit comments