Given a BST and its root, print all sequences of nodes which give rise to the same bst

前端 未结 8 974
半阙折子戏
半阙折子戏 2020-12-13 05:49

Given a BST, find all sequences of nodes starting from root that will essentially give the same binary search tree.

Given a bst, say

  3
 /  \\
1             


        
8条回答
  •  悲&欢浪女
    2020-12-13 05:59

    I have a much shorter solution. What do you think about it?

    function printSequences(root){
        let combinations = [];
    
        function helper(node, comb, others){
            comb.push(node.values);
    
            if(node.left) others.push(node.left);
            if(node.right) others.push(node.right);
    
            if(others.length === 0){
                combinations.push(comb);
                return;
            }else{
                for(let i = 0; i

提交回复
热议问题