The interviewee wrote the excellent idea to figure out the completeness of binary tree. His idea is to calculate total nodes in the binary tree first, and then assign each node with a value based on the structure of tree, the root node has value 0, and left child is parent node's value * 2 + 1, right child is parent node's value * 2 + 2. And then if it is a complete binary tree, all nodes value should be in the range of [0, totalNodes - 1]. Apply preorder travesal, and check if node's value is smaller than total nodes.
From January 2015, she started to practice leetcode questions; she trains herself to stay focus, develops "muscle" memory when she practices those questions one by one. 2015年初, Julia开始参与做Leetcode, 开通自己第一个博客. 刷Leet code的题目, 她看了很多的代码, 每个人那学一点, 也开通Github, 发表自己的代码, 尝试写自己的一些体会. She learns from her favorite sports – tennis, 10,000 serves practice builds up good memory for a great serve. Just keep going. Hard work beats talent when talent fails to work hard.
Tuesday, March 31, 2020
Case study: check completeness of binary tree
Here is the gist.
The interviewee wrote the excellent idea to figure out the completeness of binary tree. His idea is to calculate total nodes in the binary tree first, and then assign each node with a value based on the structure of tree, the root node has value 0, and left child is parent node's value * 2 + 1, right child is parent node's value * 2 + 2. And then if it is a complete binary tree, all nodes value should be in the range of [0, totalNodes - 1]. Apply preorder travesal, and check if node's value is smaller than total nodes.
The interviewee wrote the excellent idea to figure out the completeness of binary tree. His idea is to calculate total nodes in the binary tree first, and then assign each node with a value based on the structure of tree, the root node has value 0, and left child is parent node's value * 2 + 1, right child is parent node's value * 2 + 2. And then if it is a complete binary tree, all nodes value should be in the range of [0, totalNodes - 1]. Apply preorder travesal, and check if node's value is smaller than total nodes.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment