Leetcode - k-closest-points-to-origin
Leetcode - same-tree

Leetcode - subtree-of-another-tree

violet posted @ Mar 06, 2020 02:01:09 AM in 算法 with tags Algorithm tree Golang ruby , 425 阅读

https://leetcode.com/problems/subtree-of-another-tree/

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.

Example 1:
Given tree s:

     3
    / \
   4   5
  / \
 1   2

Given tree t:

   4 
  / \
 1   2

Return true, because t has the same structure and node values with a subtree of s.

This can be solved by recursive. First, it should find a node from s which has the same value as the root of t. And then check whether the sub-tree is the same as t. For finding the subtree, it can be searched from  s.Left and s.Right with recursive method.

For checking the same tree, it can be solved here.

func isSubtree(s *TreeNode, t *TreeNode) bool {
    if s == nil && t != nil {
        return false
    }
    if s != nil && t == nil {
        return true
    }
    if s.Val == t.Val {
        result := isSameTree(s, t)
        if result {
            return true
        }
    }
    
    return isSubtree(s.Left, t) || isSubtree(s.Right, t)
}

func isSameTree(s *TreeNode, t *TreeNode) bool {
    if s == nil && t == nil {
        return true
    }
    if (s != nil && t == nil) || (s == nil && t != nil) {
        return false
    }
    if s.Val != t.Val {
        return false
    }

    return isSameTree(s.Left, t.Left) && isSameTree(s.Right, t.Right)
}

 

def is_subtree(s, t)
    if s == nil && t != nil
        return false
    end
    if s != nil && t == nil
        return true
    end
    if s.val == t.val
        if is_same_tree(s, t)
            return true
        end
    end
    
    return is_subtree(s.left, t) || is_subtree(s.right, t)
end

def is_same_tree(t1, t2)
    if (t1 != nil && t2 == nil) || (t1 == nil && t2 != nil)
        return false
    end
    if t1 == nil && t2 == nil
        return true
    end
    if t1.val != t2.val
        return false
    end
    return is_same_tree(t1.left, t2.left) && is_same_tree(t1.right, t2.right)
end
SEBA 7th Class Syll 说:
Jul 14, 2023 04:19:06 PM

SEBA 7th Class Syllabus 2024 will help Students to Prepare for pass Marks in All Exams the All Subjects, Assam Students to get a Clear idea of the Topics and Subtopics and According to it they can Decide on which topic to Focus more, SEBA 7th Exam Conducted Every Year Month of March and April Months, This 6th Date Sheet 2024 Available at Official Website.as it helps Students to Plan their Preparations SEBA 7th Class Syllabus 2024 Accordingly to Meet their Expectations, we Provide Students with All the Necessary Support and Allow them to Prove their Talent by performing best in their examination, The Students skill Profile Should move From being Predominantly Receptive to Productive.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter