二叉树面试题进阶

二叉树面试题进阶

1. 二维数组存储层序遍历结果

难点:  如何存储每一层的节点 ?根据队列节点的个数判断每一层

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> retList = new ArrayList<>();
        if (root == null) {
            return retList; 
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int sz = queue.size();
            List<Integer> array = new ArrayList<>();
            while (sz != 0) {
                TreeNode cur = queue.poll();
                array.add(cur.val);
                sz--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            retList.add(array);
        }
        return retList;
    }
}

2. 找两个节点的最近公共祖先

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if (root == q || root == p) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left != null && right != null) {
            return root;
        }else if (left != null){
            return left;
        }else {
            return right;
        }
    }
}

这题还有一个更妙的解法 类似解找两个链表相交的节点

 

这种解法的难点在于如何 使用栈结构保存 r -> p,q 路径上所有节点

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    // 获得指定节点路径上所有节点
   public boolean getPathNode(Stack<TreeNode> stack, TreeNode root, TreeNode node) {
        if (root == null) {
            return false;
        }
        stack.push(root);
        if (stack.peek() == node) {
            return true;
        }
        if (getPathNode(stack,root.left,node)) {
            return true;
        }
        if (getPathNode(stack,root.right,node)) {
            return true;
        }
        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        Stack<TreeNode> stackA = new Stack<>();
        Stack<TreeNode> stackB = new Stack<>();
        getPathNode(stackA,root,p);
        getPathNode(stackB,root,q);

        if (stackA.size() > stackB.size()) {
            int sz = stackA.size() - stackB.size();
            while (sz != 0) {
                stackA.pop();
                sz--;
            }
        }else {
            int sz = stackB.size() - stackA.size();
            while (sz != 0) {
                stackB.pop();
                sz--;
            }
        }

        while (!stackA.isEmpty() && !stackB.isEmpty()) {
            if (stackA.peek() == stackB.peek()) {
                return stackA.peek();
            }else {
                stackA.pop();
                stackB.pop();
            }
        }
        return null;
    }
}

3. 二叉树创建字符串

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public String tree2str(TreeNode root) {
        StringBuilder sbu = new StringBuilder();
        tree2strChild(root,sbu);
        return sbu.toString();
    }
    public void tree2strChild(TreeNode root,StringBuilder sbu) {
        if (root == null) {
            return;
        }
        sbu.append(root.val);

        if (root.left != null) {
            sbu.append("(");
            tree2strChild(root.left,sbu);
            sbu.append(")");
        }else {
            if (root.right == null) {
                return;
            }else {
                sbu.append("()");
            }
        }

        if (root.right != null) {
            sbu.append("(");
            tree2strChild(root.right,sbu);
            sbu.append(")");
        }else {
            return;
        }
    }
}

 

posted @ 2024-01-21 13:00  qyx1  阅读(19)  评论(0)    收藏  举报