leetCode刷題——Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
題意為:判斷兩棵二叉樹是否一樣。
C程式碼實現accepted。思路:暴力比較,程式碼個人覺得寫的不夠簡潔。先比較根節點,然後左右子樹。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode *p, struct TreeNode *q) {
if(p==NULL && q==NULL) return true;
if(p!=NULL && q==NULL) return false;
if(p==NULL && q!=NULL) return false;
if(p!=NULL && q!=NULL && p->val != q->val) return false;
else
return (isSameTree(p->left,q->left))&&(isSameTree(p->right,q->right));
}
相關推薦
leetCode刷題——Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structura
LeetCode演算法題-Same Tree(Java實現)
這是悅樂書的第162次更新,第164篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第21題(順位題號是100)。給定兩個二叉樹,編寫一個函式來檢查它們是否相同。如果兩個二叉樹在結構上相同並且節點具有相同的值,則認為它們是相的。例如: 輸入: 1 1
LeetCode刷題100. Same Tree
題目: Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are
LeetCode刷題(C++)——Same Tree(Easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are c
leetcode刷題日記——Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are cons
LeetCode刷題Easy篇Minimum Depth of Binary Tree
題目 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 ne
LeetCode刷題Easy篇 Balanced Binary Tree
題目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in
LeetCode刷題Easy篇Convert Sorted Array to Binary Search Tree
題目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tr
LeetCode刷題Easy篇Binary Tree Level Order Traversal II
題目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf
C#LeetCode刷題之#104-二叉樹的最大深度(Maximum Depth of Binary Tree)
問題 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 /
LeetCode刷題Easy篇Binary Tree Paths
題目 Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / \ 2 3 \
LeetCode刷題Easy篇.Lowest Common Ancestor of a Binary Search Tree
題目 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA
LeetCode刷題EASY篇Invert Binary Tree
題目 Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9
LeetCode刷題-98——Validate Binary Search Tree(驗證搜尋二叉樹)
連結:題目:給定一個二叉樹,判斷其是否是一個有效的二叉搜尋樹。一個二叉搜尋樹具有如下特徵:節點的左子樹只包含小於當前節點的數。節點的右子樹只包含大於當前節點的數。所有左子樹和右子樹自身必須也是二叉搜尋樹
【一天一道LeetCode】#100. Same Tree(100題大關)
一天一道LeetCode 本系列文章已全部上傳至我的github,地址:ZeeCoder‘s Github 歡迎大家關注我的新浪微博,我的新浪微博 歡迎轉載,轉載請註明出處
LeetCode刷題Medium篇Construct Binary Tree from Preorder and Inorder Traversal
題目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For
LeetCode刷題Medium篇Binary Tree Zigzag Level Order Traversal
題目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the
LeetCode刷題Medium篇Lowest Common Ancestor of a Binary Tree
題目 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
LeetCode刷題MEDIM篇Implement Trie (Prefix Tree)
題目 Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie.insert("apple"); tri
【LeetCode從零單刷】Same Tree
沒錯我就是伍聲2009的粉絲,從今天起,模仿《從零單排》系列,菜雞單刷LeetCode! 題目: Given two binary trees, write a function to check if they are equal or not. Two binary