binary-tree

Find all nodes in a binary tree on a specific level (Interview Query)

落花浮王杯 提交于 2020-01-11 03:31:05
问题 I mean on a specific level, NOT up to that specific level. Could someone please check my modified BFS algorithm? (most of which is taken from Wikipedia) Queue levelorder(root, levelRequested){ int currentLevel = 0; q = empty queue q.enqueue(root) while not q.empty do{ if(currentLevel==levelRequested) return q; node := q.dequeue() visit(node) if(node.left!=null || node.right!=null){ currentLevel++; if node.left ≠ null q.enqueue(node.left) if node.right ≠ null q.enqueue(node.right) } } } 回答1: I

Finding Least Common Ancestor in Binary Tree [duplicate]

北慕城南 提交于 2020-01-10 11:46:15
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How can I find the common ancestor of two nodes in a binary tree? first common ancestor of a binary tree I have a binary tree as below. I need to find the least common ancestor (LCA). e.g LCA of 6 and 4 is 1, LCA of 4 and 5 is 2. 1 / \ 2 3 / \ / \ 4 5 6 7 Can anyone please suggest how should I approach and solve this problem? 回答1: Start with an ordinary depth-first search algorithm: public Node find(Node node,

Finding Least Common Ancestor in Binary Tree [duplicate]

谁说我不能喝 提交于 2020-01-10 11:46:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How can I find the common ancestor of two nodes in a binary tree? first common ancestor of a binary tree I have a binary tree as below. I need to find the least common ancestor (LCA). e.g LCA of 6 and 4 is 1, LCA of 4 and 5 is 2. 1 / \ 2 3 / \ / \ 4 5 6 7 Can anyone please suggest how should I approach and solve this problem? 回答1: Start with an ordinary depth-first search algorithm: public Node find(Node node,

Vertical sum of a binary tree [closed]

大憨熊 提交于 2020-01-10 09:03:50
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . How to find the vertical sum of a binary tree. For example, Consider the binary tree below, 1 / \ / \ / \ 2 3 / \ / \ / \ / \ 4 5 6 7 / \ / \ / \ / \ 5 9 1 3 6 7 5 5 For the above tree, Vertical sum should be

Vertical sum of a binary tree [closed]

旧巷老猫 提交于 2020-01-10 09:02:20
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . How to find the vertical sum of a binary tree. For example, Consider the binary tree below, 1 / \ / \ / \ 2 3 / \ / \ / \ / \ 4 5 6 7 / \ / \ / \ / \ 5 9 1 3 6 7 5 5 For the above tree, Vertical sum should be

Returning results in recursion

醉酒当歌 提交于 2020-01-07 02:43:08
问题 I am trying to solve a problem where I need to evaluate the value of a binary expression tree. tree_calc(Tree, Eval) where Eval should hold the final result and Tree must be in the form: tree(LeftNode, Operator, RightNode) If I create the tree function as per the form above, how am I supposed to pass the results of the calculations back up the recursion if there isn't an empty variable to store the results? My understanding is that an extra variable is always needed to store the result in.

Inserting node dynamically in Complete Binary Search Tree

佐手、 提交于 2020-01-06 19:24:53
问题 I know the concept of Binary Search Tree and Complete Binary tree. Is there a way we can write insert algorithm for Complete binary search tree or I am thinking of wrong data structure? My objective is every time we insert a node, Tree should remain complete binary search tree. 回答1: Of course you can. But it will be O(N) algorithm, just rebuild tree after every insertion or deletion. You can't do this faster than O(N) time. Because: 1) Exists only 1 complete tree with given keys. 2) You can

How to delete an element from a Leafy Binary Tree (Haskell)

最后都变了- 提交于 2020-01-06 05:51:19
问题 So, this tree is NOT a Binary Search Tree. It is in no particular order, and is just in this order for quick access to specific indices (nth element), rather than whether an element exists or not. The form of the Tree is like so: data Tree a = Leaf a | Node Int (Tree a) (Tree a) deriving Show For this specific tree, the "Int" from the Node constructor is the number of elements underneath that node (or number of leaves). Using this structure, I copied parts of the Tree functions available in a

Heap sort algorithm

蹲街弑〆低调 提交于 2020-01-05 09:35:22
问题 I have a heap made of a binary tree. Its not an array. I was wondering how would i go about sorting this. I know i need to take the last node and place it at the root and do a down heap bubble. This part i have. The problem I am having is knowing how to get the new last node. Is there an algorithm to find the last node? Do i need to keep track of each of the parent nodes on each node? Thanks. 回答1: Assuming the tree you start with is a full tree, I would see if you can keep track of the height

Java Binary Search Tree Recursive Copy Tree

妖精的绣舞 提交于 2020-01-05 07:42:47
问题 I'm working on a problem which requires me to copy a binary search tree recursively and to return the tree. I am coding in the binary search tree class, so it will copy whatever binary search tree it is called on. The requirements say that the private method must have a return type of Entry<E> and a parameter of type Entry<E> . The problem I'm running into is getting multiple entries added to the tree. Here is what I currently have: public BinarySearchTree<E> rcopy(){ BinarySearchTree newTree