From cded6c5c803e5d6de7a47e020e94ce03fcc68432 Mon Sep 17 00:00:00 2001 From: ExplosiveBattery <641370196@qq.com> Date: Thu, 9 Jun 2022 00:58:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md=20=20python=20code=20via=20iterate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the original method, we need to traversal every node and write the function named getDepth to get the depth of all sub trees in traverse method too. But there is more suitable uniform iteration traversal algorithm, I use the map struct in the code segment where the node is Null. If you have problem in understand, please feel free to communicate with me. --- problems/0110.平衡二叉树.md | 46 +++++++++++--------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index d98ff8a9..1b997643 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -531,40 +531,26 @@ class Solution: 迭代法: ```python class Solution: - def isBalanced(self, root: TreeNode) -> bool: - st = [] + def isBalanced(self, root: Optional[TreeNode]) -> bool: if not root: return True - st.append(root) - while st: - node = st.pop() #中 - if abs(self.getDepth(node.left) - self.getDepth(node.right)) > 1: - return False - if node.right: - st.append(node.right) #右(空节点不入栈) - if node.left: - st.append(node.left) #左(空节点不入栈) - return True - - def getDepth(self, cur): - st = [] - if cur: - st.append(cur) - depth = 0 - result = 0 - while st: - node = st.pop() + + height_map = {} + stack = [root] + while stack: + node = stack.pop() if node: - st.append(node) #中 - st.append(None) - depth += 1 - if node.right: st.append(node.right) #右 - if node.left: st.append(node.left) #左 + stack.append(node) + stack.append(None) + if node.left: stack.append(node.left) + if node.right: stack.append(node.right) else: - node = st.pop() - depth -= 1 - result = max(result, depth) - return result + real_node = stack.pop() + left, right = height_map.get(real_node.left, 0), height_map.get(real_node.right, 0) + if abs(left - right) > 1: + return False + height_map[real_node] = 1 + max(left, right) + return True ``` From e354cd6e25231fc9b255ae1d6ed3d1c58e4aa27d Mon Sep 17 00:00:00 2001 From: ExplosiveBattery <641370196@qq.com> Date: Thu, 9 Jun 2022 02:33:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md=20level=20order=20traversal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This leetcode problem can use level order traversal method, the difference between with the normal version is we should judge for None. There is my python answer, please feel free to contact with me if you have any problem. --- problems/0101.对称二叉树.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 1eb43589..caf5d249 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -437,6 +437,31 @@ class Solution: return True ``` +层次遍历 +```python +class Solution: + def isSymmetric(self, root: Optional[TreeNode]) -> bool: + if not root: + return True + + que = [root] + while que: + this_level_length = len(que) + for i in range(this_level_length // 2): + # 要么其中一个是None但另外一个不是 + if (not que[i] and que[this_level_length - 1 - i]) or (que[i] and not que[this_level_length - 1 - i]): + return False + # 要么两个都不是None + if que[i] and que[i].val != que[this_level_length - 1 - i].val: + return False + for i in range(this_level_length): + if not que[i]: continue + que.append(que[i].left) + que.append(que[i].right) + que = que[this_level_length:] + return True +``` + ## Go ```go