递归遍历二叉树

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <script>

        var tree = {
            "id": 0,
            "name": "root",
            "left": {
                "id": 1,
                "name": "Simon",
                "left": {
                    "id": 3,
                    "name": "Carl",
                    "left": {
                        "id": 7,
                        "name": "Lee",
                        "left": {
                            "id": 11,
                            "name": "Fate"
                        }
                    },
                    "right": {
                        "id": 8,
                        "name": "Annie",
                        "left": {
                            "id": 12,
                            "name": "Saber"
                        }
                    }
                },
                "right": {
                    "id": 4,
                    "name": "Tony",
                    "left": {
                        "id": 9,
                        "name": "Candy"
                    }
                }
            },
			
            "right": {
                "id": 2,
                "name": "right",
                "left": {
                    "id": 5,
                    "name": "Carl",
                },
                "right": {
                    "id": 6,
                    "name": "Carl",
                    "right": {
                        "id": 10,
                        "name": "Kai"
                    }
                }
            }
        }

        // 假设id和name均不会重复,根据输入name找到对应的id
        function findIdByName(name, tree) {
         if(tree==null){
			 return;
		 }
		 if(tree.name==name){
			 console.log(tree.id);
			 return;
		 }
		 if(tree.left!=null){
			if(tree.left.name==name){
			console.log(tree.left.id);
			return;
			}
		 }
		 if(tree.right!=null){
		 if(tree.right.name==name){
			 console.log(tree.right.id);
			 return;
		 }
		 }
		 findIdByName(name, tree.left);
		 findIdByName(name, tree.right);
		 
        }
       //  window.onload = findIdByName("Kai", tree);



        // 假设id和name均不会重复,根据输入id找到对应的name
        function findNameById(id, tree) {
            //debugger;
            if (tree == null) {
                return;
            }
            if (tree.id == id) {
                console.log(tree.name);
                return;
            }
            if (tree.left != null) {
                if (tree.left.id == id) {
                    console.log(tree.left.name);
                    return;
                }
            }
            if (tree.right != null) {
                if (tree.right.id == id) {
                    console.log(tree.right.name);
                     return;
                }
            }
            findNameById(id, tree.left);
            findNameById(id, tree.right);
        }
         window.onload = findNameById(10, tree);
        
		
		
		
        function getListWithDLR() {
            DLR(tree);
        }
        function DLR(tree) {
            if (tree == null) {
                return;
            }
            DLR(tree.left);
            console.log(tree.name);
            DLR(tree.right);
        }
        //window.onload = getListWithDLR();  // 把这个对象中所有的名字以“中序遍历”的方式全部输出到console中
      
		
		
        function getListWithLDR() {
            LDR(tree);
        }
        function LDR(tree) {
            if (tree == null) {
                return;
            }

            console.log(tree.name);
            LDR(tree.left);
            LDR(tree.right);
        }
         window.onload = getListWithLDR();  // 把这个对象中所有的名字以“前序遍历”的方式全部输出到console中
       
		
        function getListWithLRD() {
            LRD(tree);
        }

        function LRD(tree) {
            if (tree == null) {
                return;
            }
            LRD(tree.left);
            LRD(tree.right);
            console.log(tree.name);
        }
       // window.onload = getListWithLRD();// 把这个对象中所有的名字以“后序遍历”的方式全部输出到console中
    </script>
</body>

</html>

递归遍历二叉树,重在理解递归过程,以及"分而治之"的思想。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值