day15 二叉树-递归法纯享四题(height-banlanced BT, BT Paths, Left leaves count, nodes count)

### 6.10 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced
>[!Tip] **height-balanced**
>A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
```java
public class balancedBT {  
    public boolean isBalanced(TreeNode root) {  
        return getHeight(root) != -1;  
    }  
    private int getHeight(TreeNode node){  
        if(node == null) return 0;  
        int leftHeight = getHeight(node.left);  
        //如果左叶子已经不平衡了 就一路返回-1.右叶子同理。  
        if(leftHeight == -1) return -1;  
        int rightHeight = getHeight(node.right);  
        if(rightHeight == -1) return -1;  
        //如果两边差值已经大于1了 就返回-1  
        if(Math.abs(leftHeight - rightHeight) > 1) return -1;  
        //如果差值还没有大于1,则返回它的高度+1  
        return Math.max(leftHeight,rightHeight) + 1;  
    }  
}
```

### 6.11257. Binary Tree Paths
Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.
 https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html
递归+回溯
```java
public List<String> binaryTreePaths(TreeNode root) {  
    List<String> result = new LinkedList<>();  
    if(root == null) return result;  
  
    List<Integer> path = new LinkedList<>();//用于存储每条路径  
    traversal(root,path,result);  
    return result;  
}  
private void traversal(TreeNode node, List<Integer> path, List<String> res){  
    path.add(node.val);  
    StringBuilder sb = new StringBuilder();  
    if(node.left == null && node.right == null){  
        for (int i = 0; i < path.size() - 1; i++) {  
            sb.append(path.get(i) + "->");  
        }  
        sb.append(path.get(path.size()-1));  
        res.add(sb.toString());  
        return;  
    }  
    if(node.left != null){  
        traversal(node.left,path,res);  
        path.remove(path.size()-1);//返回上一个节点  
    }  
    if(node.right != null){  
        traversal(node.right,path,res);  
        path.remove(path.size()-1);  
    }  
  
}
```

### 6.12 404. Sum of Left Leaves
Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html

这题要通过父节点判断子节点是不是要的元素。即通过父节点判断子节点是不是它的左叶子,并且子节点为叶子节点。
```java
public class leftLeavesSum {  
    public int sumOfLeftLeaves(TreeNode root) {  
        if(root == null) return 0;  
        if(root.left == null && root.right == null) return 0;//从父节点取数值,节点处返回0  
        int leftSum = sumOfLeftLeaves(root.left);  
        int rightSum = sumOfLeftLeaves(root.right);  
        int mid = 0;  
        if(root.left != null && root.left.left == null && root.left.right == null){  
            mid = root.left.val;  
        }  
        return mid + leftSum + rightSum;  
    }  
}  
class leftLeavesSumTest {  
    public static void main(String[] args) {  
        TreeBuilder tb = new TreeBuilder();  
        TreeNode root = tb.buildTree();  
  
        leftLeavesSum lls = new leftLeavesSum();  
        int res = lls.sumOfLeftLeaves(root);  
        System.out.println(res);  
    }  
}
```
### 6.13  222. Count Complete Tree Nodes
Given the root of a complete binary tree, return the number of the nodes in the tree.
According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Design an algorithm that runs in less than O(n) time complexity.
https://programmercarl.com/0222.%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%8A%82%E7%82%B9%E4%B8%AA%E6%95%B0.html
完全二叉树一定是由满二叉树构成的,就算是这种情况也可以拆成三个完全二叉树:

在Java中,(2<<5) 是一个位运算符的使用示例,具体来说是左移(Left Shift)操作。这个表达式将数字 2 的二进制表示向左移动 5 位。
首先,让我们看看数字 2 的二进制表示。在二进制中,2 表示为 10(最右边的位是最低位,也就是 2^0 的位置,左边是 2^1 的位置,依此类推)。
当你将 2(即 10)向左移动 5 位时,你实际上是在其二进制表示的左侧添加了 5 个 0。因此,2<<5 的结果如下:
原始: 10  
左移5位:100000
但是,这里我们需要注意,二进制数 100000 在十进制中不是直接读作 100000(虽然它们在形式上看似相同,但在不同的数制下表示的值不同)。在十进制中,100000(二进制)等于 32(十进制)。
所以,(2<<5) 的结果是 32。
左移操作在编程中常用于快速乘以2的幂次方。例如,x<<n 相当于 x * 2^n(在x为正整数且n为非负整数的情况下,忽略可能的整数溢出)。但是,需要注意的是,如果 x 是负数或者 n 太大导致结果超出 int 或其他整数类型的表示范围,那么结果可能会是意外的。此外,如果 n 是负数,那么左移操作的行为是未定义的(Undefined Behavior, UB),在Java中通常会导致 ArithmeticException,但在实际的JVM实现中,可能会抛出不同的异常或者表现出不同的行为。然而,对于正整数 n,Java的左移操作是明确定义的。

```java
public int completeTreeNodes(TreeNode node){  
    if(node == null) return 0;  
    TreeNode left = node.left;  
    TreeNode right = node.right;  
    int leftLevel = 0;  
    int rightLevel = 0;  
    while(left != null){  
        left = left.left;  
        leftLevel++;  
    }  
    while(right != null){  
        right = right.right;  
        rightLevel++;  
    }  
    if(rightLevel == leftLevel) return (2 << leftLevel) - 1;  
  
    int leftNum = completeTreeNodes(node.left);  
    int rightNum = completeTreeNodes(node.right);  
    return leftNum + rightNum + 1;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值