mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -177,8 +177,29 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def getMinimumDifference(self, root: TreeNode) -> int:
|
||||
res = []
|
||||
r = float("inf")
|
||||
def buildaList(root): //把二叉搜索树转换成有序数组
|
||||
if not root: return None
|
||||
if root.left: buildaList(root.left) //左
|
||||
res.append(root.val) //中
|
||||
if root.right: buildaList(root.right) //右
|
||||
return res
|
||||
|
||||
buildaList(root)
|
||||
for i in range(len(res)-1): // 统计有序数组的最小差值
|
||||
r = min(abs(res[i]-res[i+1]),r)
|
||||
return r
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
@ -188,4 +209,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -160,11 +160,175 @@ Java:
|
||||
|
||||
|
||||
Python:
|
||||
```python3
|
||||
# 前序遍历-迭代-LC144_二叉树的前序遍历
|
||||
class Solution:
|
||||
def preorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
# 根结点为空则返回空列表
|
||||
if not root:
|
||||
return []
|
||||
stack = [root]
|
||||
result = []
|
||||
while stack:
|
||||
node = stack.pop()
|
||||
# 中结点先处理
|
||||
result.append(node.val)
|
||||
# 右孩子先入栈
|
||||
if node.right:
|
||||
stack.append(node.right)
|
||||
# 左孩子后入栈
|
||||
if node.left:
|
||||
stack.append(node.left)
|
||||
return result
|
||||
|
||||
# 中序遍历-迭代-LC94_二叉树的中序遍历
|
||||
class Solution:
|
||||
def inorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
if not root:
|
||||
return []
|
||||
stack = [] # 不能提前将root结点加入stack中
|
||||
result = []
|
||||
cur = root
|
||||
while cur or stack:
|
||||
# 先迭代访问最底层的左子树结点
|
||||
if cur:
|
||||
stack.append(cur)
|
||||
cur = cur.left
|
||||
# 到达最左结点后处理栈顶结点
|
||||
else:
|
||||
cur = stack.pop()
|
||||
result.append(cur.val)
|
||||
# 取栈顶元素右结点
|
||||
cur = cur.right
|
||||
return result
|
||||
|
||||
# 后序遍历-迭代-LC145_二叉树的后序遍历
|
||||
class Solution:
|
||||
def postorderTraversal(self, root: TreeNode) -> List[int]:
|
||||
if not root:
|
||||
return []
|
||||
stack = [root]
|
||||
result = []
|
||||
while stack:
|
||||
node = stack.pop()
|
||||
# 中结点先处理
|
||||
result.append(node.val)
|
||||
# 左孩子先入栈
|
||||
if node.left:
|
||||
stack.append(node.left)
|
||||
# 右孩子后入栈
|
||||
if node.right:
|
||||
stack.append(node.right)
|
||||
# 将最终的数组翻转
|
||||
return result[::-1]
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
> 迭代法前序遍历
|
||||
|
||||
```go
|
||||
//迭代法前序遍历
|
||||
/**
|
||||
type Element struct {
|
||||
// 元素保管的值
|
||||
Value interface{}
|
||||
// 内含隐藏或非导出字段
|
||||
}
|
||||
|
||||
func (l *List) Back() *Element
|
||||
前序遍历:中左右
|
||||
压栈顺序:右左中
|
||||
**/
|
||||
func preorderTraversal(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
var stack = list.New()
|
||||
stack.PushBack(root.Right)
|
||||
stack.PushBack(root.Left)
|
||||
res:=[]int{}
|
||||
res=append(res,root.Val)
|
||||
for stack.Len()>0 {
|
||||
e:=stack.Back()
|
||||
stack.Remove(e)
|
||||
node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
|
||||
if node==nil{
|
||||
continue
|
||||
}
|
||||
res=append(res,node.Val)
|
||||
stack.PushBack(node.Right)
|
||||
stack.PushBack(node.Left)
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
> 迭代法后序遍历
|
||||
|
||||
```go
|
||||
//迭代法后序遍历
|
||||
//后续遍历:左右中
|
||||
//压栈顺序:中右左(按照前序遍历思路),再反转结果数组
|
||||
func postorderTraversal(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
var stack = list.New()
|
||||
stack.PushBack(root.Left)
|
||||
stack.PushBack(root.Right)
|
||||
res:=[]int{}
|
||||
res=append(res,root.Val)
|
||||
for stack.Len()>0 {
|
||||
e:=stack.Back()
|
||||
stack.Remove(e)
|
||||
node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
|
||||
if node==nil{
|
||||
continue
|
||||
}
|
||||
res=append(res,node.Val)
|
||||
stack.PushBack(node.Left)
|
||||
stack.PushBack(node.Right)
|
||||
}
|
||||
for i:=0;i<len(res)/2;i++{
|
||||
res[i],res[len(res)-i-1] = res[len(res)-i-1],res[i]
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
> 迭代法中序遍历
|
||||
|
||||
```go
|
||||
//迭代法中序遍历
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
rootRes:=[]int{}
|
||||
if root==nil{
|
||||
return nil
|
||||
}
|
||||
stack:=list.New()
|
||||
node:=root
|
||||
//先将所有左节点找到,加入栈中
|
||||
for node!=nil{
|
||||
stack.PushBack(node)
|
||||
node=node.Left
|
||||
}
|
||||
//其次对栈中的每个节点先弹出加入到结果集中,再找到该节点的右节点的所有左节点加入栈中
|
||||
for stack.Len()>0{
|
||||
e:=stack.Back()
|
||||
node:=e.Value.(*TreeNode)
|
||||
stack.Remove(e)
|
||||
//找到该节点的右节点,再搜索他的所有左节点加入栈中
|
||||
rootRes=append(rootRes,node.Val)
|
||||
node=node.Right
|
||||
for node!=nil{
|
||||
stack.PushBack(node)
|
||||
node=node.Left
|
||||
}
|
||||
}
|
||||
return rootRes
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user