遍历树算法 - Traverse A Tree

树的遍历 Tree Traversals

   let root = {
       val: 1,
       left:{
         val: 2,
         left: {
            val: 3,
            left: {
                val: 5
            }
         },
         right: {
            val: 4,
            right: {
                val: 6
            } 
         }
       },
       right:{
           val: 7,
           left: {
               val: 8
           },
           right:{
               val: 9,
               right: {
                   val: 10
               }
           }
       } 
    };

广度优先遍历 Breadth First Traversal

层级遍历 Level Order
Algorithm Postorder(tree)
   1. Visit the root. 先访问根节点
   2. Traverse the deeper level, visit child root. call levelorder(node, depth)
   3. Continue traverse the deeper level, visit child root. call levelorder(node, depth)
   ...
 var levelOrder = function(root) {
    var orders= [];
    levelorder(root, 0)
    return orders;
    
    function levelorder(node, depth) {
        if (!node) {
            return;
        }
        orders[depth] = orders[depth] || [];
        orders[depth].push(node.val);
        levelorder(node.left, depth + 1);
        levelorder(node.right, depth + 1);
    }    
};

console.warn("levelOrder", levelOrder(root));
Output: "levelorder": [[1], [2, 7], [3, 4, 8, 9], [5, 6, 10]]

深度优先遍历 Depth First Traversals

后序遍历 Postorder (Left, Right, Root)
Algorithm Postorder(tree)
   1. Traverse the left subtree, i.e., call Postorder(left-subtree) 首先遍历左子树
   2. Traverse the right subtree, i.e., call Postorder(right-subtree) 然后遍历右子树
   3. Visit the root. 最后访问根节点
  var postorderTraversal = function(root) {
      var orders = [];
      postorder(root);
      return orders;
    
      function postorder(node) {
         if (node ) {
                if (isValidValue(node.left)) {
                    postorder(node.left);
                }
        
                if (isValidValue(node.right)) {
                    postorder(node.right);
                }
                
                if (isValidValue(node.val)) {
                    orders.push(node.val);
                } 
            }
        }
        
        function isValidValue(node) {
            return node !== null && node !== undefined && node !== "";
        }
    };
    
    console.warn("postorder", postorderTraversal(root));
Output: "postorder": [5, 3, 6, 4, 2, 8, 10, 9, 7, 1]
中序遍历 Inorder (Left, Root, Right)
Algorithm Inorder(tree)
   1. Traverse the left subtree, i.e., call Inorder(left-subtree) 首先遍历左子树
   2. Visit the root. 然后访问根节点
   3. Traverse the right subtree, i.e., call Inorder(right-subtree) 最后遍历右子树
    var inorderTraversal = function(root) {
        var orders = [];
        inorder(root);
        return orders;
    
        function inorder(node) {
            if (node ) {
                if (isValidValue(node.left)) {
                    inorder(node.left);
                }
                
                if (isValidValue(node.val)) {
                    orders.push(node.val);
                }
              
                if (isValidValue(node.right)) {
                    inorder(node.right);
                }
            }
        }
        
        function isValidValue(node) {
            return node !== null && node !== undefined && node !== "";
        }
    };
    
    console.warn("inorder", inorderTraversal(root));
    
Output: "inorder": [5, 3, 2, 4, 6, 1, 8, 7, 9, 10]
前序遍历 Preorder (Root, Left, Right)
Algorithm Preorder(tree)
   1. Visit the root. 首先访问根节点
   2. Traverse the left subtree, i.e., call Preorder(left-subtree) 然后遍历左子树
   3. Traverse the right subtree, i.e., call Preorder(right-subtree) 最后遍历右子树
``
var preorderTraversal = function(root) {
    var orders = [];
    preorder(root);
    return orders;

    function preorder(node) {
        if (node ) {
            if (isValidValue(node.val)) {
                orders.push(node.val);
            }
     
            if (isValidValue(node.left)) {
                preorder(node.left);
            }
    
            if (isValidValue(node.right)) {
                preorder(node.right);
            }
        }
    }
    
    function isValidValue(node) {
        return node !== null && node !== undefined && node !== "";
    }
};

console.warn("preorder", preorderTraversal(root));

>  Output: "preorder" [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值