0
点赞
收藏
分享

微信扫一扫

LeetCode 450.删除二叉搜索树中的节点和669.修建二叉搜索树思路对比 及heap-use-after-free问题解决


递归法:

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> res = new ArrayList<>();
        digui(root,res);
        return res;
    }

    public void digui(Node root,List<Integer> res){
        if(root!=null){
            res.add(root.val);
            for(Node child : root.children){
                digui(child,res);
            }
        }
    }
}

 

举报

相关推荐

0 条评论