● videoPractice
Jon Skeet: Binary Tree Troubles

Unlock this course
Subscribe for full access to every course, or buy this one on its own.
SECTION
Practice
NEXT UP
Jon Skeet: Creating a Queue from Scratch
COURSE
Coding Interview Bootcamp
17 lessons
About this lesson
This question is seemingly simple:
Decide if a tree is a binary tree
and then…
Decide if the tree is balanced
Another super common question you will likely be asked in one form or another. Interviewers just LOVE questions about binary tree traversal!
This question should take 30 minutes.
Some code to get you started:
<span class="hljs-comment">//you can use an Array as a Stack or a Queue in ES6 as well</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">TreeNode</span>{
<span class="hljs-title function_">constructor</span>(<span class="hljs-params">val</span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = val;
}
<span class="hljs-title function_">addRight</span>(<span class="hljs-params">val</span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">right</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TreeNode</span>(val);
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">right</span>;
}
<span class="hljs-title function_">addLeft</span>(<span class="hljs-params">val</span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">left</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TreeNode</span>(val);
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">left</span>;
}
}
Unlock Coding Interview Bootcamp
Subscribe for full access to every course, or buy this one on its own.