Operations On Tree

Solution For Operations On Tree

I’m sorry, but I cannot provide a solution to a specific problem without knowing its number or name. Could you please provide additional information or clarify your request?

Step by Step Implementation For Operations On Tree

Given the root of a binary tree, return the sum of every tree node's data.

int sum(TreeNode* root) {
    if (!root) return 0;
    return root->data + sum(root->left) + sum(root->right);
}
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root is None: 
            return 0 # Base case
        else: 
            return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
return its minimum depth = 2.
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2_     0       8
         /  \
         7   4

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Tree 
{ 
    public class TreeNode 
    { 
        public int val; 
        public TreeNode left, right; 
        public TreeNode(int val) 
        { 
            this.val = val; 
            left = right = null; 
        } 
    } 

    public class Solution 
    { 
        public List postorderTraversal(TreeNode root) 
        { 
            List result = new List(); 
            if (root == null) 
                return result; 

            Stack stack = new Stack(); 
            stack.Push(root); 

            TreeNode prev = null; 
            while (stack.Count > 0) 
            { 
                TreeNode curr = stack.Peek(); 

                // go down the tree. 
                //check if current node is leaf, if so, process it and pop stack, 
                //otherwise, keep going down
                if (prev == null || prev.left == curr || prev.right == curr) 
                { 
                    //prev == null is the situation for the root node 
                    if (curr.left != null) 
                        stack.Push(curr.left); 
                    else if (curr.right != null) 
                        stack.Push(curr.right); 
                    else
                    { 
                        stack.Pop(); 
                        result.Add(curr.val); 
                    } 
                } 
                //go up the tree from left node    
                //need to check if there is a right child 
                //if yes, push it to stack 
                //otherwise, process parent and pop stack
                else if (curr.left == prev) 
                { 
                    if (curr.right != null) 
                        stack.Push(curr.right); 
                    else
                    { 
                        stack.Pop(); 
                        result.Add(curr.val); 
                    } 
                } 
                //go up the tree from right node 
                //after coming back from right node, process parent node and pop stack. 
                else if (curr.right == prev) 
                { 
                    stack.Pop(); 
                    result.Add(curr.val); 
                } 

                prev = curr; 
            } 

            return result; 
        } 
    } 
}
Scroll to Top

Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]