package main
func main() {
}
//Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
// 938. Range Sum of BST 二叉搜索树的范围和
//Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 Output: 32
//Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 Output: 23
func rangeSumBST(root *TreeNode, L int, R int) int {
if root == nil {
return 0
}
if root.Val > R {
return rangeSumBST(root.Left, L, R)
}
if root.Val < L {
return rangeSumBST(root.Right, L, R)
}
return root.Val + rangeSumBST(root.Left, L, R) + rangeSumBST(root.Right, L, R)
}
// 617. Merge Two Binary Trees 合并二叉树
//Input:
// Tree 1 Tree 2
// 1 2
// / \ / \
// 3 2 1 3
// / \ \
// 5 4 7
//Output:
// Merged tree:
// 3
// / \
// 4 5
// / \ \
// 5 4 7
func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
if t1 == nil { // 如果t1为空,t2非空,那么我们就以t2的结点值建立一个新结点
return t2
}
if t2 == nil { // 如果t2为空,t1非空,那么我们就以t1的结点值建立一个新结点
return t1
}
// 如果t1和t2都非空,那么我们就以t1和t2的结点值之和建立一个新结点,然后分别对t1的左右子结点和t2的左右子结点调用递归函数
return &TreeNode{t1.Val + t2.Val, mergeTrees(t1.Left, t2.Left), mergeTrees(t1.Right, t2.Right)}
}
// 104. Maximum Depth of Binary Tree 求二叉树最大深度
// 思路:很简单,当前结点深度等于左右子树中较大的那个深度加一。
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := maxDepth(root.Left)
right := maxDepth(root.Right)
if left > right {
return left + 1
} else {
return right + 1
}
}
//求二叉树最小深度
//算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度;
//如果不都存在,就需要返回左右子树的最大深度(因为子节点不存在的话,通向该子树的路径就走不同,就不存在深度,也无法比较。
//只能从另一子树方向走。)如果左右孩子不都存在时还取小值,那返回的就是父节点的深度,会出错。
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := minDepth(root.Left)
right := minDepth(root.Right)
if left == 0 || right == 0 {
return left + right + 1
}
if left > right {
return right + 1
} else {
return left + 1
}
}
// 226. Invert Binary Tree 反转二叉树
// 思路:递归互换左右子节点
//Example:
//
//Input: Output:
//
// 4 4
// / \ / \
// 2 7 7 2
// / \ / \ / \ / \
// 1 3 6 9 9 6 3 1
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return root
}
root.Left, root.Right = invertTree(root.Right), invertTree(root.Left)
return root
}
// TODO 判断两棵树是否相等
func isEqual(r1, r2 *TreeNode) bool {
if r1 == nil && r2 == nil {
return true
}
if r1 == nil || r2 == nil {
return false
}
if r1.Val == r2.Val {
return isEqual(r1.Left, r2.Right) && isEqual(r1.Right, r2.Right)
}
return false
}
// 538. Convert BST to Greater Tree 二叉查找树转化为更大树
// 思路:二叉查找树右边子节点比节点数值大,递归所有右节点的累加和 右-中-左遍歷相加
func convertBST(root *TreeNode) *TreeNode {
node, _ := traverse(root, 0)
return node
}
func traverse(root *TreeNode, sum int) (*TreeNode, int) {
if root == nil {
return nil, sum
}
_, sum = traverse(root.Right, sum)
root.Val += sum
sum = root.Val
_, sum = traverse(root.Left, sum)
return root, sum
}
// 230.给定一个二叉搜索树,请找出其中第k小的节点。
// 中序遍历
func kthSmallest(root *TreeNode, k int) int {
if root == nil {
return 0
}
var sum, num int
var inOrder func(root *TreeNode)
inOrder = func(root *TreeNode) {
if root == nil {
return
}
inOrder(root.Left)
num++
if num == k {
sum = root.Val
return
}
inOrder(root.Right)
}
inOrder(root)
return sum
}
// 给定一棵二叉搜索树,请找出其中第k大的节点。
// 中序遍历反着来:后-中-左
func kthLargest(root *TreeNode, k int) int {
var num, sum int
var inOrder func(root *TreeNode)
inOrder = func(root *TreeNode) {
if root == nil {
return
}
inOrder(root.Right)
num++
if num == k {
sum = root.Val
return
}
inOrder(root.Left)
}
inOrder(root)
return sum
}
// 98. 给定一个二叉树,判断其是否是一个有效的二叉搜索树。
// 中序遍历
var lastNode *TreeNode
func isValidBST(root *TreeNode) bool {
lastNode = nil
return inOrder(root)
}
func inOrder(root *TreeNode) bool {
if root == nil {
return true
}
if !inOrder(root.Left) {
return false
}
if lastNode != nil && lastNode.Val >= root.Val {
return false
}
lastNode = root
return inOrder(root.Right)
}
// 55. 给定一个二叉树,判断该树是不是平衡二叉树
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
if maxDepth(root.Left) - maxDepth(root.Right) > 1 || maxDepth(root.Right) - maxDepth(root.Left) > 1 {
return false
}
return isBalanced(root.Left) && isBalanced(root.Right)
}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left, right := maxDepth(root.Left), maxDepth(root.Right)
if left > right {
return left + 1
}
return right + 1
}
// 从上到下按层打印二叉树
//例如:
//给定二叉树: [3,9,20,null,null,15,7],
// 3
// / \
// 9 20
// / \
// 15 7
//返回其层次遍历结果:
//
//[
//[3],
//[9,20],
//[15,7]
//]
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
var arr [][]int
q := []*TreeNode{root}
for len(q) > 0 {
temp := make([]int, len(q))
length := len(q)
for ; length >= 0; length-- {
if q[0].Left != nil {
q = append(q, q[0].Left)
}
if q[0].Right != nil {
q = append(q, q[0].Right)
}
temp = append(temp, q[0].Val)
q = q[1:]
}
arr = append(arr, temp)
}
return arr
}
// 按照之字形打印二叉树
// 第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,依次类推
func printTreeNode(root *TreeNode) [][]int {
if root == nil {
return nil
}
var arr [][]int
q := []*TreeNode{root}
for level := 0; len(q) > 0; level++ {
length := len(q)
temp := make([]int, length)
if level&0x1 == 0 {
for i := 0; i < length; i++ {
temp = append(temp, q[i].Val)
}
} else {
for i := length; i >= 0; i-- {
temp = append(temp, q[i].Val)
}
}
arr = append(arr, temp)
for ; length >= 0; length-- {
if q[0].Left != nil {
q = append(q, q[0].Left)
}
if q[0].Right != nil {
q = append(q, q[0].Right)
}
q = q[1:]
}
arr = append(arr, temp)
}
return arr
}