funcpreorderTraversal(root *TreeNode) []int { var f func(root *TreeNode) []int var res []int f = func(root *TreeNode) []int { // 当前节点为 nil,返回 if root == nil{ return res }
// 先将当前节点加入答案数组 res = append(res, root.Val) // 遍历左子树 if root.Left != nil{ f(root.Left) } // 遍历右子树 if root.Right != nil{ f(root.Right) } return res } f(root) return res }
funcinorderTraversal(root *TreeNode) []int { var f func(*TreeNode) []int var res []int f = func(root *TreeNode) []int{ if root == nil{ return res } f(root.Left) res = append(res, root.Val) f(root.Right) return res } f(root) return res }
迭代实现:与前序遍历类似,可以用一个栈结构实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funcinorderTraversal(root *TreeNode) []int { var stack []*TreeNode var res []int var node = root
for node != nil || len(stack) > 0{ for node != nil{ stack = append(stack, node) node = node.Left } res = append(res, stack[len(stack) - 1].Val) if stack[len(stack) - 1].Right != nil{ node = stack[len(stack) - 1].Right } stack = stack[:len(stack) - 1] } return res }
funcfindBottomLeftValue(root *TreeNode)int { // 答案,以及现在遍历的最大一层 var ans, maxLevel int var dfs func(*TreeNode, int) dfs = func(node *TreeNode, level int){ if node == nil{ return }
if level > maxLevel{ // 这一层第一个遍历到的就是最左边的元素 ans = node.Val } maxLevel = max(level, maxLevel)
dfs(node.Left, level+1) dfs(node.Right, level+1) } dfs(root, 1) return ans }
funcmax(x, y int)int{ if x > y { return x } return y }
funcmaxDepth(root *TreeNode)int { var res int var f func(*TreeNode, int) f = func(root *TreeNode, depth int){ if root == nil{ return } res = max(res, depth) f(root.Left, depth + 1) f(root.Right, depth + 1) } f(root, 1) return res }
funcmax(x, y int)int{ if x > y{ return x } return y }
funcisSameTree(p *TreeNode, q *TreeNode)bool { var res = true var f func(p *TreeNode, q *TreeNode) f = func(p *TreeNode, q *TreeNode) { if p == nil && q == nil{ return } if p == nil || q == nil{ res = false return } if p.Val != q.Val{ res = false return } f(p.Left, q.Left) f(p.Right, q.Right) }
funcisSymmetric(root *TreeNode)bool { var res = true var f func(p *TreeNode, q *TreeNode) f = func(p *TreeNode, q *TreeNode) { if p == nil && q == nil{ return } if p == nil || q == nil{ res = false return } if p.Val != q.Val{ res = false return } f(p.Left, q.Right) f(p.Right, q.Left) }
funcmaxAncestorDiff(root *TreeNode)int { var dfs func(*TreeNode, int, int) var res int dfs = func(root *TreeNode, maxInt, minInt int){ if root == nil{ return }
funcdelNodes(root *TreeNode, to_delete []int) []*TreeNode { var element = make(map[int]struct{}) for i := range to_delete{ element[to_delete[i]] = struct{}{} }
var dfs func(*TreeNode, bool) *TreeNode var ans []*TreeNode dfs = func(node *TreeNode, isParent bool) *TreeNode { if node == nil{ returnnil }
if _, ok := element[node.Val]; ok{ // 需要删除 dfs(node.Left, true) dfs(node.Right, true)
funcisValidBST(root *TreeNode)bool { var f func(*TreeNode, int, int)bool f = func(node *TreeNode, left, right int)bool { if node == nil{ returntrue } x := node.Val return x > left && x < right && f(node.Left, left, x) && f(node.Right, x, right) }
funclcaDeepestLeaves(root *TreeNode) *TreeNode { // 左右子树深度相同,那么这个就是它们的公共祖先 var maxLevel int var f func(*TreeNode, int)int var ans *TreeNode f = func(node *TreeNode, level int)int{ if node == nil{ maxLevel = max(maxLevel, level) return level }
left := f(node.Left, level+1) right := f(node.Right, level+1)
if left == right && left == maxLevel{ ans = node } return max(left, right) } f(root, 0) return ans }
funcmax(x, y int)int{ if x > y{ return x } return y }