JZ55 二叉树的深度
题源 👉 二叉树的深度_牛客题霸_牛客网 (nowcoder.com)
题目描述:
具体实现:
方法一:
public int TreeDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
}
时间:O(n)
空间:O(n)
JZ27 二叉树的镜像
题源 👉 二叉树的镜像_牛客题霸_牛客网 (nowcoder.com)
题目描述:
具体实现:
方法一:
public TreeNode Mirror (TreeNode pRoot) {
if(pRoot == null) return null;
if(pRoot.left != null || pRoot.right != null){
TreeNode temp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = temp;
}
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
时间:O(n)
空间:O(n)