Java编程计算二叉树的深度(二叉树的遍历)

昨天面试拼多多,现场给了一道求解数的宽度的题目。回来想着要把有关树的算法整理一下,

1.首先是计算树的深度:

public class Solution { 
    public int TreeDepth(TreeNode root) { 
        if(root==null)
            return 0;
        int left=TreeDepth(root.left)+1;
        int right=TreeDepth(root.right)+1;
        return Math.max(left,right);
    }
}

2.层序遍历二叉树

public class Solution { 
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { 

        ArrayList<Integer> list=new ArrayList<Integer>();
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        if(root==null) return list;
        
        list.add(root.val);
        queue.add(root);
        
        while(!queue.isEmpty())
        { 
            TreeNode node=queue.peek();
            if(node.left!=null)
            { 
                list.add(node.left.val);
                queue.add(node.left);
            }
            if(node.right!=null)
            { 
                list.add(node.right.val);
                queue.add(node.right);
            }
            queue.remove();
        }
        return list;
    }
}

3.二叉树前序遍历

 // 前序
    public static void preOrder() { 

        BinTreeNode p = createBinTree();

        Stack<BinTreeNode> stack = new Stack<BinTreeNode>();

        while (p != null || !stack.empty()) { 
            if (p != null) { 
                System.out.print(p.data);
                stack.push(p);
                p = p.lchild;
            }
            else { 
                p = stack.pop();
                p = p.rchild;
            }
        }
        System.out.println();

    }

    // 前序递归
    public static void preOrderRecursion(BinTreeNode top) { 
        if (top != null) { 
            System.out.println(top.data);
            preOrderRecursion(top.lchild);
            preOrderRecursion(top.rchild);
        }
    }

4.二叉树中序遍历

// 中序
    public static void inOrder() { 

        BinTreeNode p = createBinTree();

        Stack<BinTreeNode> stack = new Stack<BinTreeNode>();

        while (p != null || !stack.empty()) { 
            if (p != null) { 
                stack.push(p);
                p = p.lchild;
            }
            else { 
                p = stack.pop();
                System.out.print(p.data);
                p = p.rchild;
            }
        }
        System.out.println();
    }

    // 中序递归
    public static void inOrderRecursion(BinTreeNode top) { 
        if (top != null) { 
            inOrderRecursion(top.lchild);
            System.out.println(top.data);
            inOrderRecursion(top.rchild);
        }
    }

5.后序遍历

 // 后序
    public static void postOrder() { 

        BinTreeNode p = createBinTree();

        Stack<BinTreeNode> stack = new Stack<BinTreeNode>(); // 初始化栈

        int mark = 1; // 转向标志
        while (p != null || !stack.empty()) {  // 遍历
            if (p != null && mark != 0) { 
                stack.push(p);
                p = p.lchild;
            }// 转向左子树
            else { 
                p = stack.pop();
                p.flag++; // 退栈
                if (p.flag == 1) { 
                    stack.push(p);
                    p = p.rchild;
                    mark = 1;
                } // 转向右子树
                else if (p.flag == 2 && !stack.empty()) {  // 输出结点
                    System.out.print(p.data);
                    mark = 0;
                }
                else if (p.flag == 2 && stack.empty()) {  // 输出根结点并退出
                    System.out.print(p.data);
                    break;
                }
            } // if-else
        } // while
        System.out.println();
    }

    // 后序递归
    public static void postOrderRecursion(BinTreeNode top) { 
        if (top != null) { 
            postOrderRecursion(top.lchild);
            postOrderRecursion(top.rchild);
            System.out.println(top.data);
        }
    }

    原文作者:__Forever__
    原文地址: https://blog.csdn.net/qq_16118409/article/details/99212218
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞