class TreeNode {
constructor(val) {
this.val = val;
this.left = this.right = null;
}
}
function init(){
var root = new TreeNode(5)
var node1 = new TreeNode(4)
var node2 = new TreeNode(8)
var node3 = new TreeNode(11)
var node4 = new TreeNode(13)
var node5 = new TreeNode(4)
var node6 = new TreeNode(7)
var node7 = new TreeNode(2)
var node8 = new TreeNode(1)
root.left = node1
root.right = node2
node1.left = node3
node2.left = node4
node2.right = node5
node3.left = node6
node3.right = node7
node5.right = node8
return root
}
var root = init()
init后得到这样一棵二叉树
如何遍历才能得到这样的数据?????
var result = [
[5, 4, 11, 7],
[5, 4, 11, 2],
[5, 8, 13],
[5, 8, 4, 1],
]
即按照分支左右分支遍历(描述可能有问题,大概是这个意思)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…