二叉樹的各種演算法面試題及答案解析
阿新 • • 發佈:2019-01-11
前言
下面的所有面試題及解析答案都是經過驗證的。
面試題
樹的定義
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
計算樹的結點數量(遞迴)
樹的結點數量等於左子樹加上右字數的數量+1
public class Solution {
public int sumOfNode(TreeNode root) {
if (root==null){
return 0;
}
int leftSum=sumOfNode(root.left);
int rightSum=sumOfNode(root.right);
return leftSum+rightSum+1;
}
}
計算樹的結點數量(非遞迴)
使用佇列,取出上層結點的同時,將下層左右子節點存入
public class Solution {
public int sumOfNode(TreeNode root) {
if(root== null){
return 0;
}
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
int sum=0;
while(!queue.isEmpty()){
TreeNode node=queue.poll();
sum++;
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!= null){
queue.offer(node.right);
}
}
return sum;
}
}
樹的深度計算(非遞迴)
import java.util.Queue;
import java.util.LinkedList;
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null){
return 0;
}
if(root.left==null&&root.right==null){
return 1;
}
int currentLevelNodes=1; //記錄當前層的結點數量
int nextLevelNodes=0; //記錄下一層結點的數量
int depth=0; //記錄樹的深度
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
TreeNode node=queue.poll(); //取出一個結點,記錄該結點的下一層結點,這也是queue的使用原因
currentLevelNodes--;
if(node.left!=null){
queue.offer(node.left);
nextLevelNodes++;
}
if(node.right!=null){
queue.offer(node.right);
nextLevelNodes++;
}
if(currentLevelNodes==0){
currentLevelNodes=nextLevelNodes;
nextLevelNodes=0;
depth++;
}
}
return depth;
}
}
樹的深度計算(遞迴)
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null){
return 0;
}
if(root.left==null&&root.right==null){
return 1;
}
int leftLength=TreeDepth(root.left);
int rightLength=TreeDepth(root.right);
return Math.max(leftLength,rightLength)+1;
}
}
從上向下遍歷樹
記得使用佇列 Queue!!
import java.util.Queue;
import java.util.LinkedList;
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> array=new ArrayList<Integer>();
if(root==null){
return array;
}
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
TreeNode node=queue.poll();
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
array.add(node.val);
}
return array;
}
}
前序遍歷(遞迴)
這裡的陷阱在於
ArrayList<Integer> array=new ArrayList<Integer>();
變數array的使用
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> array=new ArrayList<Integer>();
if(root==null){
return array;
}
preorderTraversal(root,array);
return array;
}
public void preorderTraversal(TreeNode root,ArrayList<Integer> array){
if(root==null){
return;
}
array.add(root.val);
preorderTraversal(root.left,array);
preorderTraversal(root.right,array);
}
}
前序遍歷(非遞迴)
使用棧,對於每一個結點來說,先將它的右節點放進去,再將左節點放進去,然後迴圈拿出棧頂元素,這樣可以做到執行完所有的左子樹才能輪上右子樹。
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> array=new ArrayList<Integer>();
if(root==null){
return array;
}
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.pop();
array.add(node.val);
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
return array;
}
}
中序遍歷(遞迴)
這裡的陷阱在於
ArrayList<Integer> array=new ArrayList<Integer>();
變數array的使用
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> array=new ArrayList<Integer>();
if(root==null){
return array;
}
inorderTraversal(root,array);
return array;
}
public void inorderTraversal(TreeNode root,ArrayList<Integer> array){
if(root==null){
return;
}
inorderTraversal(root.left,array);
array.add(root.val);
inorderTraversal(root.right,array);
}
}
後序遍歷(遞迴)
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> array=new ArrayList<Integer>();
if(root==null){
return array;
}
postorderTraversal(root,array);
return array;
}
public void postorderTraversal(TreeNode root,ArrayList<Integer> array){
if(root==null){
return;
}
postorderTraversal(root.left,array);
postorderTraversal(root.right,array);
array.add(root.val);
}
}
二叉樹中和為某一值的路徑(遞迴)
public class Solution {
ArrayList<ArrayList<Integer>> array=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> temp=new ArrayList<Integer>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root==null){
return array;
}
temp.add(root.val);
target-=root.val;
if(target==0&&root.left==null&&root.right==null){
array.add(new ArrayList(temp)); //建立新的ArrayList
}
FindPath(root.left,target);
FindPath(root.right,target);
temp.remove(temp.size()-1); //深度優先演算法回退一步
return array;
}
}
leetCode:sum-root-to-leaf-numbers
題目表述:
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.
public class Solution {
public int sumNumbers(TreeNode root) {
if(root==null){
return 0;
}
if(root.left==null&&root.right==null){
return root.val;
}
int sum=0;
return sumNumbers(root,sum);
}
public int sumNumbers(TreeNode root,int sum){
if(root==null){
return 0;
}
sum=10*sum+root.val;
if(root.left==null&&root.right==null){ //必須加這個,不然返回都是0
return sum;
}
int sumLeft=sumNumbers(root.left,sum);
int sumRight=sumNumbers(root.right,sum);
return sumLeft+sumRight;
}
}