We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
树状数据结构的遍历在我们日常工作中是十分常见的遍历方式,无奈每次对树状结构进行遍历,笔者总感觉不是那么得心应手。例如:
因此,找了一些时间,进行复习。这里先从二叉树的遍历看起。
这里的“序”,实际上指的是根节点的访问顺序。 先序,即根->左->右 中序,即左->根->右 后序,即左->右->根 无论根在何处,左节点一定在右节点之前访问。
94. 二叉树的中序遍历 617. 合并二叉树
The text was updated successfully, but these errors were encountered:
这是一个二叉树的树状结构的数据,每个子节点中均包含key,若存在子节点则包含children。children中第0个元素是左节点,第1个元素是右节点。我们来将树中的每一个节点按照先序、中序、后序遍历的顺序来输出。
let aTree = { key: 'A', children: [ { key: 'B', children: [ { key: 'D', children: [ { key: 'G' }, { key: 'H' } ] }, null ] }, { key: 'C', children: [ { key: 'E', children: [ null, { key: 'I' } ] }, { key: 'F' } ] }, ] }
首先,我们来编写先序遍历的代码。
function preOrderTraverse(biTree) { if (!biTree) return console.log(biTree.key) biTree.children && preOrderTraverse(biTree.children[0]) biTree.children && preOrderTraverse(biTree.children[1]) }
执行代码,得到遍历结果:
执行过程:函数开始执行后,首先输出当前遍历的树的根节点,然后递归地分别对左孩子和右孩子也进行这个操作。
Sorry, something went wrong.
No branches or pull requests
了解
树状数据结构的遍历在我们日常工作中是十分常见的遍历方式,无奈每次对树状结构进行遍历,笔者总感觉不是那么得心应手。例如:
因此,找了一些时间,进行复习。这里先从二叉树的遍历看起。
这里的“序”,实际上指的是根节点的访问顺序。
先序,即根->左->右
中序,即左->根->右
后序,即左->右->根
无论根在何处,左节点一定在右节点之前访问。
挑战
94. 二叉树的中序遍历
617. 合并二叉树
The text was updated successfully, but these errors were encountered: