题意:判断非空二叉树t是否为非空二叉树s的子树。
分析:对于树s的root结点来说,子树t:(1)与以root为根的树相等(2)在root->left子树中(3)在root->left子树中
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool StartRoot(TreeNode* s, TreeNode* t){ if(s == NULL && t == NULL) return true; if(s == NULL || t == NULL) return false; if(s -> val != t -> val) return false; return StartRoot(s -> left, t -> left) && StartRoot(s -> right, t -> right); } bool isSubtree(TreeNode* s, TreeNode* t) { if(s == NULL) return false; return isSubtree(s -> left, t) || isSubtree(s -> right, t) || StartRoot(s, t); } };