二叉树题目汇总

来源:互联网 发布:mysql 命令删除表字段 编辑:程序博客网 时间:2024/06/05 04:32

二叉树题目汇总

二叉树的遍历(前序、中序、后序遍历和层次遍历)
重建二叉树
二叉树镜像
二叉树的深度
二叉树的宽度
判断二叉搜索树的后序遍历是否合法
判断一颗二叉树是否是平衡二叉树
判断一棵二叉树是否为完全二叉树
二叉树中和为某一值的路径
将二叉树搜索树转化为双向链表
求二叉树第 k 层结点个数
求二叉树两个结点的最低公共祖先结点
求二叉树中两个结点的最大距离
序列化和反序列化二叉树

  • 普通树中的祖先结点到后代结点的路径
  • 二叉树根结点到所有叶子节点的路径

普通树中的祖先结点到后代结点的路径

题目:
给出树的一个结点以及它的一个后代结点,输出从祖先结点到后代结点的路径。
如下面的树中,给出 0 和 7 两个结点,则输出0到7的路径 0, 1, 4, 7。

             0            / \          1    2        /  \       3    4     / \   / | \    5  6  7  8  9
package algorithm.foroffer.top50;import org.junit.Test;import java.util.ArrayList;import java.util.List;/** * description: * * @author liyazhou * @create 2017-06-05 21:23 */class TreeNode{    int             value;    List<TreeNode>  children = new ArrayList<>();    public TreeNode(int _value){ value = _value; }    public void addChild(TreeNode child){ children.add(child); }    public void addChildren(TreeNode... children){        for (TreeNode child : children) this.children.add(child);    }}public class Test50_2 {    /**     * 找根结点到目标结点的路径     * @param root 树的根结点     * @param target 树中的一个结点     * @param path 从根结点到目标结点的路径     * @return 如果存在从根结点到目标结点的路径,则返回;否则返回 null     */    public boolean getNodePath(TreeNode root, TreeNode target, List<TreeNode> path){        // 添加当前结点        path.add(root);        System.out.print(root.value + "\t");  //0   1   3   5   6   4   7        if (root == target) return true;        boolean found = false;        // 处理孩子结点        for (TreeNode child : root.children){            found = getNodePath(child, target, path);            if (found) break;        }        // 以当前结点为根结点的子树中,不存在目标结点,则从路径中删除该结点        if (!found) path.remove(path.size()-1);        return found;    }    @Test    public void test(){        TreeNode[] nodes = generateTree();        TreeNode root = nodes[0];        TreeNode target1 = nodes[1];        List<TreeNode> path1 = new ArrayList<>();        getNodePath(root, target1, path1);        System.out.println();        for(TreeNode node : path1) System.out.print(node.value + "\t");        System.out.println();    }    /**     *               0     *              / \     *            1    2     *          /  \     *         3    4     *       / \   / | \     *      5  6  7  8  9     *     */    private TreeNode[] generateTree() {        TreeNode node0 = new TreeNode(0);        TreeNode node1 = new TreeNode(1);        TreeNode node2 = new TreeNode(2);        TreeNode node3 = new TreeNode(3);        TreeNode node4 = new TreeNode(4);        TreeNode node5 = new TreeNode(5);        TreeNode node6 = new TreeNode(6);        TreeNode node7 = new TreeNode(7);        TreeNode node8 = new TreeNode(8);        TreeNode node9 = new TreeNode(9);        node0.addChildren(node1, node2);        node1.addChildren(node3, node4);        node3.addChildren(node5, node6);        node4.addChildren(node7, node8, node9);        return new TreeNode[]{node0, node7};    }}

二叉树根结点到所有叶子节点的路径

package ac.leetcode.tree;import org.junit.Test;import java.util.LinkedList;import java.util.List;/** * description: * * @author liyazhou * @since 2017-07-02 10:45 * *  Binary Tree Paths * * 题目: *  Given a binary tree, return all root-to-leaf paths. *  For example, given the following binary tree: *                 1 *               /   \ *              2     3 *               \ *                5 * * All root-to-leaf paths are:  ["1->2->5", "1->3"] */public class BinaryTreePaths {    private static class BinTreeNode{        int value;        BinTreeNode left;        BinTreeNode right;        public BinTreeNode (int _value){ value = _value; }        public void setChildren(BinTreeNode _left, BinTreeNode _right){            left = _left;            right = _right;        }    }    public List<String> binaryTreePaths(BinTreeNode root){        List<String> paths = new LinkedList<>();        if (root == null) return paths; // if root == null, return []        String path = "";        binaryTreePaths(root, paths, path);        return paths;    }    private void binaryTreePaths(BinTreeNode root, List<String> paths, String path) {        if (root == null) return;        // 叶子结点,路径中的末尾结点,是专有的        if (root.left == null && root.right == null){            if ("".equals(path))  path += root.value;            else                  path += "->" + root.value;            paths.add(path);            return;        }        // 根结点到当前结点的路径,为其左右子结点公有的路径        if ("".equals(path)) path += root.value;        else                 path += "->"+root.value;        binaryTreePaths(root.left, paths, path);        binaryTreePaths(root.right, paths, path);    }    @Test    public void test(){        BinTreeNode node1 = new BinTreeNode(1);        BinTreeNode node2 = new BinTreeNode(2);        BinTreeNode node3 = new BinTreeNode(3);        BinTreeNode node5 = new BinTreeNode(5);        node1.setChildren(node2, node3);        node2.setChildren(null, node5);        List<String> paths = binaryTreePaths(node1);        for (String path : paths)            System.out.println(path);    }}