
深度:根节点到节点的距离:根节点深度为1。最底下的节点(叶子节点)深度为3
高度:任意一个节点到叶子节点的距离。3那个节点的高度为3,15的高度为1
求高度:后序遍历。把中间的处理过程返回给父节点
求深度:前序遍历
- class Solution(object):
- def maxDepth(self, root):
- """
- :type root: TreeNode
- :rtype: int
- """
- return self.getDepth(root)
- #求最大深度(根系节点到叶子节点的距离)==求根节点的高度
- #所以这道题用后序遍历
- #终止条件
- def getDepth(self,node):
- if node is None:
- return 0
- #单层逻辑(左右中)
- leftsize = self.getDepth(node.left)
- rightsize = self.getDepth(node.right)
- #因为往上传值
- midsize = max(leftsize,rightsize)+1
- return midsize
题目链接/文章讲解/视频讲解: 代码随想录
层序遍历的ac了,递归法没有ac。递归法没有搞清楚怎么来的数字,后面知道时终止条件得到的数字
- class Solution:
- def maxDepth(self, root: Optional[TreeNode]) -> int:
- #迭代法,层序遍历
- if root is None:
- return 0
- que = deque()
- que.append(root)
- depth = 0
-
- while que:
- size = len(que)
- while size > 0:
- node = que.popleft()
- if node.left:
- que.append(node.left)
- if node.right:
- que.append(node.right)
- size -= 1
- depth += 1
- return depth
- var maxDepth = function(root) {
- //递归法写,后序遍历
- const traversal = function(node){
- if(node === null){
- return 0
- }
- let left = traversal(node.left)
- let right = traversal(node.right)
- let mid = Math.max(left,right)+1
- return mid
- }
- return traversal(root)
-
- };
- class Solution(object):
- def maxDepth(self, root):
- """
- :type root: Node
- :rtype: int
- """
- return self.getDepth(root)
- #求最大深度=求高度,使用后序遍历
- def getDepth(self,node):
- #终止条件
- if not node:
- return 0
- #左右中,如果遇上children怎么办?
- #原来是
- # leftsize = self.getDepth(node.left)
- # rightsize = self.getDepth(node.right)
- #因为往上传值
- # midsize = max(leftsize,rightsize)+1
- #children 合并在在一起了,所以使用遍历,但是又不能比较,所以使用depth
- depth = 0
- for i in range(len(node.children)):
- depth = max(self.getDepth(node.children[i]),depth)
- return depth +1
- class Solution:
- def maxDepth(self, root: 'Node') -> int:
- if root is None:
- return 0
- que = deque()
- que.append(root)
- depth = 0
- while que:
- size = len(que)
- while size > 0:
- node = que.popleft()
- if node.children:
- for i in range(len(node.children)):
- if node.children[i]:
- que.append(node.children[i])
- size -= 1
- depth += 1
- return depth
递归法:
- class Solution(object):
- def minDepth(self, root):
- """
- :type root: TreeNode
- :rtype: int
- """
- return self.getDepth(root)
- def getDepth(self,node):
- if not node:
- return 0
- #单层逻辑,求最小深度==求高度,使用后序遍历,左右中
- leftsize = self.getDepth(node.left)
- rightsize = self.getDepth(node.right)
- #如果碰到左边是空的话,我们不可以直接比较大小取最小值,因为永远是等于0
- if node.left is None and node.right is not None:
- return rightsize + 1
- if node.left is not None and node.right is None:
- return leftsize + 1
- #都不为空
- depth = min(leftsize,rightsize)+1
- return depth
迭代法:
- class Solution(object):
- def minDepth(self, root):
- if root is None:
- return 0
- que = deque()
- depth = 0
- que.append(root)
- while len(que)!=0:
- size = len(que)
- depth += 1
- while size >0:
- node = que.popleft()
- if not node.left and not node.right:
- return depth
- if node.left is not None:
- que.append(node.left)
- if node.right is not None:
- que.append(node.right)
- size -= 1
- return depth
题目链接/文章讲解/视频讲解:代码随想录
三刷
- var minDepth = function(root) {
- // 最小深度
- const traversal = function(node){
- if(node === null){
- return 0
- }
- let left = traversal(node.left)
- let right = traversal(node.right)
- // 如果有一边为空
- if(node.left === null && node.right !== null){
- return right+1
- }
- if(node.right === null && node.left !== null){
- return left+1
- }
- // 都不为空
- let mid = Math.min(left,right)+1
- return mid
- }
- return traversal(root)
- };
- class Solution(object):
- def countNodes(self, root):
- if root is None:
- return 0
- que = deque()
- count = 0
- que.append(root)
- while len(que)!=0:
- size = len(que)
- while size >0:
- count += 1
- node = que.popleft()
- if node.left is not None:
- que.append(node.left)
- if node.right is not None:
- que.append(node.right)
- size -= 1
- return count
二叉树做法
- class Solution(object):
- def countNodes(self, root):
- """
- :type root: TreeNode
- :rtype: int
- """
-
- #满二叉树满足2的深度次方-1
- if root is None:
- return 0
- #使用指针
- left = root.left
- right = root.right
- leftdepth = 0
- rightdepth = 0
- while left is not None:
- left = left.left
- leftdepth += 1
- while right is not None:
- right = right.right
- rightdepth += 1
- if leftdepth == rightdepth:
- #因为depth从0开始的
- return pow(2,leftdepth+1) -1
- #左右中
- return self.countNodes(root.left)+self.countNodes(root.right) +1
当遇到了非满二叉树的,就继续往下遍历,直到叶子节点(他都指向null,所以是满)
题目链接/文章讲解/视频讲解:代码随想录
二刷:
层序遍历法:(ac)
- class Solution:
- def countNodes(self, root: Optional[TreeNode]) -> int:
- #层序遍历
- if root is None:
- return 0
- que = deque()
- que.append(root)
- count = 0
- while que:
- size = len(que)
- while size>0:
- node = que.popleft()
- count += 1
- if node.left:
- que.append(node.left)
- if node.right:
- que.append(node.right)
- size -= 1
- return count
递归遍历
- class Solution:
- def countNodes(self, root: Optional[TreeNode]) -> int:
- #后序遍历 左右中,中间返回节点个数
- def recursion(node):
- #参数,返回值,终止条件,单层递归逻辑
- if not node:
- return 0
- left = recursion(node.left)
- right = recursion(node.right)
- mid = left + right + 1
- return mid
- if root is None:
- return 0
- return recursion(root)
Js:
- var countNodes = function(root) {
- // 递归法
- const recursion = function(node){
- // 后序遍历
- if(node===null){
- return 0;
- }
- let leftsize = recursion(node.left);
- let rightsize = recursion(node.right);
- let midsize = leftsize + rightsize + 1;
- return midsize;
- }
- return recursion(root);
- };
- var countNodes = function(root) {
- // 层序遍历
- if(root === null){
- return 0;
- }
- let que = []
- que.push(root)
- let count = 0
- while(que.length){
- let size = que.length;
- while(size--){
- let node = que.shift();
- count ++;
- node.left && que.push(node.left);
- node.right && que.push(node.right);
- }
- }
- return count;
-
- };
使用完全二叉树的属性写的
- var countNodes = function(root) {
- if(root === null){
- return 0
- }
- let leftdepth = 1;
- let rightdepth = 1;
- let left = root.left;
- let right = root.right;
- while(left){
- leftdepth ++;
- left = left.left;
- console.log('leftdepth',leftdepth);
- }
- while(right){
- rightdepth ++;
- right = right.right;
- console.log('rightdepth',rightdepth);
- }
- if(leftdepth===rightdepth){
- console.log("euqal")
- return Math.pow(2,leftdepth)-1
- }
- return countNodes(root.left)+countNodes(root.right)+1;
-
- };